package main

import (
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"time"
)

// main
//
//	@Description: 返回的是html ,html中对数据的渲染 ，详见demo03/index.html
func main() {

	engine := gin.Default()

	//配置模板目录,多层目录
	engine.LoadHTMLGlob("templates/demo03/*")

	engine.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK,
			"demo03/index.html", //在对应的html标记上名称，此处使用该名称,需要对应上
			gin.H{
				"time":  time.Now().Format("2006-01-02 15:04:05"), //传入到对应的html页面 {{ .time }}
				"week":  time.Now().Weekday().String(),            //传入到对应的html页面 {{ .week }}
				"score": rand.Intn(100),                           //<!--条件判断-->
				"hobby": []string{"唱歌", "跳舞", "打篮球"},              //<!--循环遍历切片-->
				"newsList": []News{ //<!--循环遍历结构体切片-->
					{
						UserName: "张三",
						Address:  "北京",
						Thing:    "打架",
						Time:     time.Now().Format("2006-01-02 15:04:05"),
					},
					{
						UserName: "李四",
						Address:  "上海",
						Thing:    "打篮球",
						Time:     time.Now().Format("2006-01-0215:04:05"),
					},
					{
						UserName: "王五",
						Address:  "重庆",
						Thing:    "吃火锅",
						Time:     time.Now().Format("2006-01-0215:04:05"),
					},
				},
				"testSlice": []string{}, //<!--循环遍历切片2-空切片-->
				"news": &Article{ //<!--with解构结构体-->
					Title:   "这是标题&&&&",
					Content: "这是内容****",
				},
			},
		)
	})

	engine.Run(":8888")
}

type News struct {
	UserName string `json:"userName"`
	Address  string `json:"address"`
	Thing    string `json:"thing"`
	Time     string `json:"time"`
}

type Article struct {
	Title   string `json:"title"`
	Content string `json:"content"`
}
